package com.omi.iberolactalpreventa;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class Credenciales extends Activity{
	
	private TextView txtPhoneId;
	private TextView txtClaveAdmin;
	private Toast tCambiosOk;
	
	@SuppressLint("NewApi")
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_credenciales);
		
		txtPhoneId= (TextView) findViewById(R.id.txtIdSmartPhone);
		txtClaveAdmin=(TextView) findViewById(R.id.txtClaveAdmin);
				
				//Cargamos el Id del smartphone;
				Cursor c = Modulebase.db.ejecutarSelect(new String[]{"PHONE_ID"}, "CONFIGURACION", null,null);
		if(c!=null && c.moveToFirst())
		{
			txtPhoneId.setText(c.getString(0));
		}
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	@Override
	public void onBackPressed() {
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.credenciales,menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		  if (item.getItemId() == android.R.id.home) {
		        finish();
		        return true;
		    }
	    switch (item.getItemId()) {
	    case R.id.btnMenuAceptar:
	    	//Guardamos los datos de los cambios:
	    	if(Modulebase.db.ejecutarNonQuery("UPDATE CONFIGURACION SET PASSWORD='"+txtClaveAdmin.getText()+"',PHONE_ID='"+txtPhoneId.getText()+"' WHERE USUARIO='ADMIN'"))
	    	{
	    		tCambiosOk = Toast.makeText(this,"Cambios guardados correctamente", Toast.LENGTH_SHORT);
	    		tCambiosOk.show();
	    		finish();
	    		return true;
	    	}
	    	tCambiosOk = Toast.makeText(this,"Error al modificar los datos", Toast.LENGTH_SHORT);
    		tCambiosOk.show();
	    	return false;
	    }
	    return false;
	}
}
